package client.comm.baoding.ui

import android.content.ClipData
import android.content.ClipboardManager
import android.view.View
import androidx.lifecycle.Observer
import client.comm.commlib.toast.toast
import client.comm.commlib.widget.EmptyLayout
import client.comm.baoding.R
import client.comm.baoding.databinding.ActivityWldetailBinding

import client.comm.baoding.ui.vm.WlDetailViewModel
import client.comm.baoding.BaseMiddleActivity

class WlDetailActivity : BaseMiddleActivity<WlDetailViewModel, ActivityWldetailBinding>() {

    override fun getLayout(): Int {
        return R.layout.activity_wldetail
    }

    override fun initView() {
        binding?.apply {
            event = this@WlDetailActivity
            vm = viewModel
        }
    }

    override fun observerUI() {
        binding.emptyLayout.setOnClickListener {
            intent.getStringExtra("orderId")?.let {
                viewModel.getData(it)
            }
        }
        viewModel.call.observe(this, Observer {
            it?.let {
                if (it.isSuccess) {
                    it.data?.let {
                        binding.logisticsName = it.logistics_name
                        binding.logisticsNum = it.logistics_num
                        it?.context_list?.let {
                            binding.stepperWrap.setBBB(it)
                        }
                    }
                    binding.emptyLayout.visibility = View.GONE
                } else {
                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
                }
            }
        })
    }

    override fun initData() {
        intent.getStringExtra("orderId")?.let {
            viewModel.getData(it)
        }
    }

    fun fz(str: String?) {
        if (!str.isNullOrEmpty()) {
            val cmb = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
            cmb.setPrimaryClip(ClipData.newPlainText(null, str))
            toast("已复制")
        } else {
            toast("复制失败")
        }
    }

    override fun initViewModel(): WlDetailViewModel = createViewModel()
}